<?php

require_once dirname(__FILE__) . '/../lib/proveedoresGeneratorConfiguration.class.php';
require_once dirname(__FILE__) . '/../lib/proveedoresGeneratorHelper.class.php';

/**
 * proveedores actions.
 *
 * @package    tercermilenio
 * @subpackage proveedores
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class proveedoresActions extends autoProveedoresActions {

    protected function processForm(sfWebRequest $request, sfForm $form) {
        //var_dump($request->getParameter($form->getName()));exit();
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';

            if ($form->getObject()->isNew()) {
                $existe = ProveedoresPeer::proveedorExiste($form->getValue('pvd_nro_documento'));
            } else {
                $existe = ProveedoresPeer::proveedorExiste($form->getValue('pvd_nro_documento'), $form->getValue('pvd_id'));
            }

            if (!$existe) {
                $proveedores = $form->save();


                $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $proveedores)));

                if ($request->hasParameter('_save_and_add')) {
                    $this->getUser()->setFlash('notice', $notice . ' You can add another one below.');

                    $this->redirect('@proveedores_new');
                } else {
                    $this->getUser()->setFlash('notice', $notice);

                    $this->redirect('@proveedores');
                }
            } else {
                $this->getUser()->setFlash('error', 'El documento ingresado ya existe. Por favor, ingrese uno distinto.', false);
                //$this->redirect(array('sf_route' => 'proveedores_edit', 'sf_subject' => $proveedores));
            }
        } else {
            $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
        }
    }

    public function executeBackToMenu() {
        $this->redirect('@proveedores');
    }

    public function executeLocalidadesPorProvincia(sfWebRequest $request) {

        $localidades = null;
        $this->pciaId = $request->getParameter('pcia_id');
        $this->locId = $request->getParameter('loc_id');

        if (!empty($this->pciaId)) {

            $localidades = LocalidadesPeer::getLocalidadesPorProvincia($this->pciaId);
        } else {
            $localidades = LocalidadesPeer::doSelect(new Criteria());
        }
        $this->localidades = $localidades;
    }

    public function executeObtenerProvincia(sfWebRequest $request) {
        if ($request->hasParameter('loc_id')) {
            $this->loc_id = $request->getParameter('loc_id');
        }

        $this->localidad = LocalidadesPeer::retrieveByPk($this->loc_id);

        $this->provincias = ProvinciasPeer::doSelect(new Criteria());
        if (!empty($this->localidad)) {
            $this->provincia = ProvinciasPeer::retrieveByPk($this->localidad->getPciaId());
        }
    }


}
